/**
 * 版权所有:(C)2001-2012 北京中油瑞飞信息技术有限责任公司
 * Author : 李科风
 * Creation time : 2014年8月13日 下午1:48:27
 */
package com.bigknow.appstore.common.rpc;

public class RPCClassLoader extends ClassLoader {

    public RPCClassLoader() {
        super(Thread.currentThread().getContextClassLoader());
    }

    public synchronized Class<?> defineClass(String name, byte[] b) {
        try {
            Class<?> clz = _loadClass(name, 50);
            return clz;
        } catch (ClassNotFoundException e) {
        }
        return super.defineClass(name, b, 0, b.length);
    }

    public Class<?> loadClass(String name) throws ClassNotFoundException {
        return _loadClass(name, 1);
    }

    private Class<?> _loadClass(String name, int count) throws ClassNotFoundException {
        try {
            return super.loadClass(name);
        } catch (ClassNotFoundException e) {
            if (name.startsWith("com.bigknow.appstore") && count < 50) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e1) {
                }
                return _loadClass(name, count++);
            } else {
                throw e;
            }
        }
    }
}
